package com.linln.agent.member;

import javax.servlet.http.HttpServletRequest;

import com.linln.common.utils.ToolUtil;
import com.linln.modules.system.domain.Area;
import com.linln.modules.system.service.AreaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.linln.common.constant.AdminConst;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.vo.ResultVo;
import com.linln.modules.member.domain.MemberAgent;
import com.linln.modules.member.domain.MemberAgentMoney;
import com.linln.modules.member.domain.MemberInfo;
import com.linln.modules.member.service.MemberAgentMoneyService;
import com.linln.modules.member.service.MemberAgentService;

import java.util.List;

/**
 * @author hct
 * @date 2019/09/12
 */
@Controller
@RequestMapping("/agent/agentmoney")
public class AgentMemberMoneyController {

    @Autowired
    private MemberAgentMoneyService memberAgentMoneyService;
    @Autowired
    private MemberAgentService memberAgentService;
    @Autowired
    private AreaService areaService;


    /**
     * 列表页面
     */
    @GetMapping("/index")
    public String index(Model model,  HttpServletRequest request) {
    	MemberInfo agent=(MemberInfo)request.getSession().getAttribute(AdminConst.AGENT_ROLE_NAME);
        MemberAgent memberagent=(MemberAgent)request.getSession().getAttribute(AdminConst.AGENT_AREA_NAME); 
        MemberAgentMoney membermoney=new MemberAgentMoney();        
    	model.addAttribute("agent",agent);
    	model.addAttribute("memberagent",memberagent);    	
    	membermoney.setArea(memberagent.getArea());
    	MemberAgent memberagentnew=new MemberAgent();
    	memberagentnew.setId(memberagent.getId());
     	membermoney.setMemberagent(memberagentnew); 
    	// 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching();
        // 获取数据列表
        Example<MemberAgentMoney> example = Example.of(membermoney, matcher);
        Page<MemberAgentMoney> list = memberAgentMoneyService.getPageList(example);
        List<MemberAgentMoney> content = list.getContent();
        for (MemberAgentMoney memberAgentMoney :content
             ) {
            if(memberAgentMoney==null) continue;
            Area area = areaService.findByCode(memberAgentMoney.getArea());
            if(area != null){
                Area parea = areaService.getById(area.getPid());
                memberAgentMoney.setArea(area.getName());
                if(parea != null){
                    memberAgentMoney.setArea(parea.getName()+area.getName());
                }
            }
        }
        // 封装数据
        model.addAttribute("list", content);
        model.addAttribute("page", list);
        return "/agent/member/agentmoney/index";
    }

    /**
     * 跳转到申请页面
     */
    @GetMapping("/toapply")
    public String toapply(Model model, HttpServletRequest request) {
    	 MemberAgent memberagent=(MemberAgent)request.getSession().getAttribute(AdminConst.AGENT_AREA_NAME); 
    	 memberagent=memberAgentService.getById(memberagent.getId());
         model.addAttribute("memberagent",memberagent);

        return "/agent/member/agentmoney/apply";
    }
    /**
     * 申请提现
     * @param
     */
    @PostMapping("/apply")
    @ResponseBody
    public ResultVo approval( MemberAgentMoney membermoney, HttpServletRequest request) {
        // 复制保留无需修改的数据
        if(membermoney==null){
            return ResultVoUtil.error("提现信息有异常，请联系管理员");
        }
    	if(membermoney.getCurmoney()<500D) {
            return ResultVoUtil.error("申请提现额度不能低于500元");
    	}
    	membermoney.setStatus(0);
    	MemberAgent memberagent=(MemberAgent)request.getSession().getAttribute(AdminConst.AGENT_AREA_NAME);
        if(memberagent==null){
            return ResultVoUtil.error("请重新登陆");
        }
        MemberAgent memberagentnew=memberAgentService.getById(memberagent.getId());
        if(membermoney.getCurmoney()>memberagentnew.getCurmoney()) {
            return ResultVoUtil.error("申请提现额度不能超过您的当前总额");
    	}
    	membermoney.setArea(memberagentnew.getArea());
     	membermoney.setMemberagent(memberagentnew);
        membermoney.setOrderCode(ToolUtil.getRandomString(32));
        membermoney = memberAgentMoneyService.saveTixian(membermoney);
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 跳转到详细页面
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("member:memberInfo:detail")
    public String toDetail(@PathVariable("id") MemberAgentMoney memberAgent, Model model) {
         model.addAttribute("memberAgent",memberAgent);
        return "/agent/member/agentmoney/detail";
    }

  
}